<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_tag.php');
require ('./configs/site_config.php');

class Tag
{
	var $tag_c;
	function __construct(){
	
	$this->tag_c = new TagControl();
	}
	//标签管理
	function index()
	{
		$w_type = empty($_REQUEST['w_type']) ? 0 : (int)$_REQUEST['w_type'];//网站类型
		if($_REQUEST['jax_type'])
		{
			switch ($_REQUEST['jax_type'])
			{
				case 'del_tag'://删除tag
					if(!empty($_GET['checkList']))
					{
						$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
						$this->tag_c->delete_tag($checkList);
					}
				break;
				
				case 'update_tag_name'://修改tag名称
					if(!empty($_GET['tag_id'])) {
						$this->tag_c->change_info((int)$_GET['tag_id'],trim($_GET['tag_name']),'tag_name','update_tag');
						$this->updateTemplate((int) $_GET['tag_id'], $w_type, 'ajax') ;
					}
				break;
				
				case 'update_hot'://修改是否热门
					if(!empty($_GET['tag_id'])) {
						$this->tag_c->change_info((int)$_GET['tag_id'],(int)$_GET['value'],'is_hot','update_tag');
						$this->updateTemplate((int) $_GET['tag_id'], $w_type, 'ajax') ;
					}
				break;
				
				case 'update_click_count'://修改点击数
					if(!empty($_GET['tag_id'])) {
						$this->tag_c->change_info((int)$_GET['tag_id'],(int)$_GET['tag_click_count'],'click_count','update_tag');
					}
				break;
				
				case 'is_repeat'://tag名称是否重复
					echo $this->tag_c->change_info((int)$_GET['tag_id'],trim($_GET['tag_name']),'tag_name','is_repeat_tag',(int)$_GET['w_type']);
				break;
			}
			exit;
		}elseif($_REQUEST['act'] == 'add')//增加
		{
			if($_POST['tag_name'])
			{
				$affect = $this->tag_c->operate_tag('add',0,($_POST['tag_name']),(int)$_POST['click_count']
				,$_POST['is_hot'],$_POST['tag_type'],$w_type);
				if($affect == 1)
				{
				echo  '<script type="text/javascript"> 
				alert("增加成功！")
				</script>';
				}
			}
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('tile',     '增加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->display('tag_info.htm'); 
		}elseif($_REQUEST['act'] == 'edit')//修改
		{
			$tag_info = array();
			if($_POST['tag_name'])
			{
				if(!empty($_POST['tag_id']))
				{
					$affect = $this->tag_c->operate_tag('edit',(int)$_POST['tag_id'],($_POST['tag_name']),(int)$_POST['click_count']
					,$_POST['is_hot'],$_POST['tag_type'],$_POST['tag_web']);
					if($affect == 1)
					{
						echo  '<script type="text/javascript">alert("修改成功！");</script>';
						
						$this->updateTemplate((int) $_POST['tag_id'], $w_type, 'no_jax') ;
					}
				}
			}
			$tag_info = $this->tag_c->get_tag_id_info((int)$_GET['tag_id']);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('tag_info',$tag_info[0] );
			$GLOBALS['smarty']->display('tag_info.htm'); 
		}else{
			
			if(!empty($w_type))
			{
			
				$url_info = $tag_info = array();
				//分页处理
				$page = 1;
				if (!empty($_REQUEST['page'])) {
					$page = (int)$_REQUEST['page'];
				}
				if($_POST['search']||$_REQUEST['url_page'] == 1)
				{
					$tag_name = $_REQUEST['url_page'] == 1 ? urldecode($_REQUEST['tag_name']):$_REQUEST['tag_name'];
					$url_info = $this->tag_c->get_url_info($w_type,$page,15,2,$_REQUEST['is_hot'],$_REQUEST['tag_type'],
					$tag_name);
					$tag_info = $this->tag_c->get_tag_info($w_type,'list_s',$url_info['page_info']['start'],15
					,$_REQUEST['is_hot'],$_REQUEST['tag_type'],$tag_name);
					
					$GLOBALS['smarty']->assign('is_hot',   (int)$_REQUEST['is_hot']);
					$GLOBALS['smarty']->assign('tag_name',  stripslashes($_REQUEST['tag_name']));
				}else
				{
					$url_info = $this->tag_c->get_url_info($w_type,$page,15,1);
					$tag_info = $this->tag_c->get_tag_info($w_type,'list',$url_info['page_info']['start'],15);
				}
				$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
				$GLOBALS['smarty']->assign('w_type',   $w_type);
				$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
				$GLOBALS['smarty']->assign('tag_info',$tag_info );
				$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
				$GLOBALS['smarty']->assign('site_config', $GLOBALS['site_config'][$w_type]['tag'] );
				$GLOBALS['smarty']->display('tag_index.htm'); 
			}
		}	
	}

	/**
	* 修改与发布模板操作
	* @param   int      $tag_id  标签ID
	* @param   int      $w_type   网站标识
	* @param   int      $type     操作类型
	* @return  string
	*/
	function updateTemplate($tag_id = 0, $w_type, $type = '') {
		
		//定义更新分类模板
		$update_arr = array('tag_id' => $tag_id,
							'w_type' => (int) $w_type,
							'operation_type' => 'tag',
							'location' => '/wondershare/tag.php?action=index&act=edit&tag_id='.$tag_id.'&w_type='.$w_type,				
		);
		
		if ($type == 'ajax') {
	
			$update_arr['is_ajax'] = 1;
			
			file_get_contents("http://cms.wondershare.com/wondershare/template.php?action=updateTemplate&type=update&".
								http_build_query($update_arr));
		} else {
			
			echo '<script type="text/javascript">
					window.location.href="/wondershare/template.php?action=updateTemplate&type=update&'.http_build_query($update_arr).'";		
				  </script>';
		}
	}
	
	function get_tag_name()
	{
		$d_id=intval($_REQUEST['id']);
		$result=$this->tag_c->get_tag_name($d_id);
		$taglist=explode(",",$result);
		echo  json_encode($taglist);
	}
}

$action    = $_REQUEST['action']; 
$tag   = new Tag;
if(method_exists($tag, $action)) 
{
	$tag->$action();
}else{
	$tag->index();
}



?>
